package com.hw.common.utils;

import com.alibaba.fastjson.JSONObject;
import com.hw.common.framework.plugin.json.ExceptionErrorListener;
import com.hw.common.framework.plugin.json.JSONReader;
import com.hw.common.framework.plugin.json.JSONValidatingReader;
import com.hw.common.framework.plugin.json.parser.JsonConverter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/*****
 * <pre>
 * 类名称：JSONTools
 * 类描述：JSON转换工具类
 * 创建人：JokenWang
 * 创建时间：2014年12月30日 下午2:19:53
 * </pre>
 * 
 * @version 1.0.0
 */
public final class JsonUtil
{
    public static <T> T convertJson2BeanObject(String json, Class<T> beanClass)
    {
        return JSONObject.parseObject(json, beanClass);
    }
    
    public static <T> List<T> convertJson2BeanArray(String json, Class<T> beanClass)
    {
        return JSONObject.parseArray(json, beanClass);
    }
    
    public static String convertBean2Json(Class<?> beanClass)
    {
        return JSONObject.toJSONString(beanClass);
    }
    
    public static Map<Object, Object> convertJson2Map(String rsp)
    {
        JSONReader reader = new JSONValidatingReader(new ExceptionErrorListener());
        Object jsonRoot = reader.read(rsp);
        if (jsonRoot instanceof Map<?, ?>)
        {
            Map<?, ?> rootJson = (Map<?, ?>) jsonRoot;
            Map<Object, Object> json = new HashMap<Object,Object>();
            Set<?> keySet = rootJson.keySet();
            Object[] keys = keySet.toArray();
            for (int i = 0; i < keys.length; i++)
            {
                if (rootJson.get(keys[i]) instanceof String)
                {
                    Map<Object, Object> map = new HashMap<Object,Object>();
                    map.put(keys[i], rootJson.get(keys[i]));
                    json.putAll(map);
                }
                if (rootJson.get(keys[i]) instanceof Map<?, ?>)
                {
                    Map<?, ?> jsonMap = (Map<?, ?>) rootJson.get(keys[i]);
                    json.putAll(jsonMap);
                }
                if (rootJson.get(keys[i]) instanceof List<?>)
                {
                    List<?> jsonList = (List<?>) rootJson.get(keys[i]);
                    json.put(keys[i], jsonList);
                }
            }
            try
            {
                return JsonConverter.fromJson(json, HashMap.class);
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
        
        return null;
    }
    
    // public static void main(String[] args) throws IntrospectionException
    // {
    // String json =
    // "{\"itemList\":[{\"barCode\":\"4453535335\",\"batchMode\":\"OM\",\"brandId\":\"100001\",\"brandName\":\"拳王\",\"categoryId\":\"368\",\"categoryName\":\"眼部保养\",\"comment\":0,\"inventory\":\"566\",\"inventoryFlag\":\"1\",\"inventoryMode\":\"OM\",\"itemId\":\"10744\",\"itemName\":\"拳王系列28888889990005555\",\"itemNo\":\"444333\",\"mainImgUrl\":\"http://10.167.3.154/group2/M00/14/2A/CqcDnlTUG1eAOGwIAAGdDf4yWoQ521.jpg\",\"multiple\":\"5\",\"omUnit\":\"封\",\"otMultiple\":\"5\",\"otUnit\":\"封\",\"outerSpecifications\":\"5555\",\"price\":\"12.0\",\"saleTime\":\"2015-02-06 17:43:47\",\"supplierId\":\"10244\",\"supplierName\":\"cf供货商\",\"updateTime\":\"2015-02-06 17:43:56\",\"zonePrice\":\"0.0\",\"zonePriceCount\":\"1\"},{\"barCode\":\"6953042700206\",\"brandId\":\"101741\",\"brandName\":\"爱时乐\",\"comment\":0,\"itemId\":\"10755\",\"itemName\":\"爱时乐巧克力威化卷心酥50克/盒\",\"itemNo\":\"113510\",\"mainImgUrl\":\"http://10.167.3.154/group1/M00/13/B0/CqcDnVS3iD-ATfS8AAAu6Wtt_K4487.jpg\",\"multiple\":\"36\",\"omUnit\":\"箱\",\"otMultiple\":\"36\",\"otUnit\":\"盒\",\"price\":\"0.0\",\"saleTime\":\"2015-02-06 09:47:43\",\"supplierId\":\"10284\",\"supplierName\":\"liuzhenfeng的供应商\",\"updateTime\":\"2015-02-06 09:47:59\",\"zonePrice\":\"0.0\",\"zonePriceCount\":\"0\"},{\"barCode\":\"67575757575\",\"batchMode\":\"OM\",\"brandId\":\"100001\",\"brandName\":\"拳王\",\"categoryId\":\"251\",\"categoryName\":\"桌布围裙\",\"comment\":0,\"inventory\":\"2123\",\"inventoryFlag\":\"1\",\"inventoryMode\":\"OM\",\"itemId\":\"10741\",\"itemName\":\"拳王系列22222222222222222222我2222222222222222222我2222222222222222222我2222222222222222222我2222222222222222222我\",\"itemNo\":\"345679\",\"mainImgUrl\":\"http://10.167.3.154/group1/M00/13/DB/CqcDnVTUkk2ADvtvAABtCaX3D2o277.jpg\",\"multiple\":\"8909\",\"omUnit\":\"封\",\"otMultiple\":\"8909\",\"otUnit\":\"封\",\"outerSpecifications\":\"2222222222222222222我\",\"price\":\"985588.0\",\"saleTime\":\"2015-02-05 17:03:13\",\"supplierId\":\"10244\",\"supplierName\":\"cf供货商\",\"updateTime\":\"2015-02-05 17:03:13\",\"zonePrice\":\"0.0\",\"zonePriceCount\":\"0\"},{\"barCode\":\"6902088104103\",\"batchMode\":\"OM\",\"brandId\":\"101094\",\"brandName\":\"力士\",\"comment\":0,\"inventory\":\"4656\",\"inventoryFlag\":\"1\",\"inventoryMode\":\"OM\",\"itemId\":\"10740\",\"itemName\":\"力士200毫升强韧深层修复洗发乳/支\",\"itemNo\":\"113531\",\"mainImgUrl\":\"http://10.167.3.154/group2/M00/13/D7/CqcDnlS3iA2AQ7a0AAAOC5ArODY398.jpg\",\"multiple\":\"12\",\"omUnit\":\"箱\",\"otMultiple\":\"12\",\"otUnit\":\"支\",\"price\":\"444.0\",\"saleTime\":\"2015-02-05 16:53:10\",\"supplierId\":\"10244\",\"supplierName\":\"cf供货商\",\"updateTime\":\"2015-02-05 16:53:38\",\"zonePrice\":\"0.0\",\"zonePriceCount\":\"0\"},{\"barCode\":\"6996663333\",\"batchMode\":\"OM\",\"brandId\":\"100018\",\"brandName\":\"可口可乐/果粒橙\",\"categoryId\":\"369\",\"categoryName\":\"卸妆\",\"comment\":0,\"inventory\":\"5465\",\"inventoryFlag\":\"1\",\"inventoryMode\":\"OM\",\"itemId\":\"10742\",\"itemName\":\"可口可乐/果粒橙系列19哇哇哇哇推特一样人一样\",\"itemNo\":\"567876\",\"mainImgUrl\":\"http://10.167.3.154/group1/M00/13/DB/CqcDnVTUlV6AWv4aAASmGuEF8G0493.jpg\",\"multiple\":\"555\",\"omUnit\":\"盒\",\"otMultiple\":\"555\",\"otUnit\":\"台\",\"outerSpecifications\":\"一样\",\"price\":\"456.0\",\"saleTime\":\"2015-02-05 16:52:44\",\"supplierId\":\"10244\",\"supplierName\":\"cf供货商\",\"updateTime\":\"2015-02-05 16:53:11\",\"zonePrice\":\"0.0\",\"zonePriceCount\":\"1\"}],\"pageNo\":\"1\",\"pageSize\":\"5\",\"totalCount\":\"36\"}";
    // List<?> list = (List<?>)JSONTools.convertJson2Map(json).get("itemList");
    // System.out.println(((Map<?, ?>)list.get(0)).get("saleTime"));
    // }
}
